﻿@{
    ViewBag.Title = "仓库容器明细账";
    Layout = "~/Views/Shared/_ManufactureList.cshtml";
}
<link href="~/Content/css/ContainerLedgerDetails.css" rel="stylesheet" />
<link href="~/Content/css/ContainerLedgerDetails_print.css" rel="stylesheet" media="print" />
<script type="text/javascript">
    $(function () {
        Index.init();
    });

    var Index = {
        defaultDate: '@ViewData["defaultDate"].ToString()',
        isDefaultDate:@Html.Raw(ViewData["isDefaultDate"].ToString().ToLower()),
        jsonData:@Html.Raw(ViewData["jsonData"].ToString()),
        curUnit:'@ViewData["curUnit"].ToString()',
        curUserName:'@ViewData["curUserName"].ToString()',
        curDate:'@ViewData["curDate"].ToString()',
        curTime:'@ViewData["curTime"].ToString()', 
        printItemTemplate: "",
        init: function () {
            with (Index) {
                printItemTemplate = $("#print .page").html();

                initControl();

                loadDataList();

                $('select').bindSelect();
            }
        },
        initControl: function () {
            var cols = "FContainerNO,FContainerName,FPrice,FHighestStore,FLowestStore".split(",");
            
            cols.forEach(function(col){
                col in Index.jsonData &&  $("#{0}".format(col)).val(Index.jsonData[col]);
            });

            var strDate = "{0}-{1}-1".format(Index.defaultDate.substr(0,4),Index.defaultDate.substr(4,2)); 

            $("h1").text("{0}仓库容器明细账".format(new Date(strDate).format("yyyy年MM月")));

            $("#butnBack1").on("click", function () {
                $.goBack();
            });
            //打印
            $("#butnPrint1").on("click", function () {
                $("#print").jqprint();
            });
            //导出
            $("#butnExport1").on("click", function () {
                var param = { id: "V_RQK_14_MX_StoConListAcco_Mid1", param: " and FAccountDate = '{0}' and FContainerNO = '{1}'".format(Index.defaultDate,Index.jsonData["FContainerNO"]) };

                !Index.isDefaultDate && (param["id"] = "V_RQK_16_MX_StoConListAcco_Arc1");

                var url = "/Common/ExcelExportById?{0}".format($.param(param));

                location.href = url;
            });
        },
        loadDataList: function () {
            var url = "/WareHouse/Container/GetContainerLedgerDetails", postData = $.buildPostParam("FContainerNO");

            postData["FAccountDate"] = Index.defaultDate;

            var $gridList1 = $("#gridList1");

            $gridList1.dataGridForJson({
                url: url,
                height: $(window).height() - 420,
                postData: postData,
                colModel: [
                 { label: '日期', name: 'FCurDate', width: 80, align: 'left', sortable: false },
                { label: '单据编号', name: 'FDocuNO', width: 100, align: 'left', sortable: false },
                { label: '发生单位', name: 'FHappenComNO', width: 100, align: 'right', sortable: false },
                { label: '发生单位名称', name: 'FHappenComName	', width: 160, align: 'right', sortable: false },
                { label: '凭证编号', name: 'Fcredence', width: 100, align: 'left', sortable: false },
                { label: 'ColFQuantity_QCS', name: 'FQuantity_QC', width: 80, align: 'right', sortable: false,hidden:true },
                { label: '期初', name: 'QC', width: 60, align: 'right', sortable: false, align: 'right'},
                { label: '入库', name: 'FQuantity_RK', width: 60, align: 'right', sortable: false },
                { label: '出库', name: 'FQuantity_CK', width: 60, align: 'right', sortable: false },
                { label: '废品', name: 'FQuantity_BS', width: 60, align: 'right', sortable: false },
                { label: '返修', name: 'FQuantity_FX', width: 60, align: 'right', sortable: false },
                { label: '盘亏', name: 'FQuantity_PC', width: 60, align: 'right',  sortable: false },
                { label: '结存数量', name: 'JC', width: 100, align: 'right', sortable: false },
                { label: '单价<br/>(元)', name: 'FPrice', width: 60, align: 'right', sortable: false },
                { label: '结存金额<br/>(元)', name: 'FAmount', width: 80, align: 'right', sortable: false },
                { label: '', name: '', width: $(window).width() - 1230, align: 'left', sortable: false }
                ],
                pager: "#gridPager1",
                sortname: 'FAuditingDate,FAuditingTime,FHappenComNO',
                sortorder: "asc,asc,asc",
                viewrecords: true,
                rownumWidth: 50,
                onSelectRow: function (rowId) {

                },
                loadComplete: function (data) {
                    Index.dataList = data;
                    
                    var columnInfos = {
                        FCurDate:"日期",
                        FHappenComName:"发生单位名称",
                        ZY:"摘要",
                        FQuantity_QC:"期初",
                        FQuantity_RK:"入库",
                        FQuantity_CK:"出库",
                        FQuantity_BS:"废品",
                        FQuantity_FX:"返修",
                        FQuantity_PC:"盘亏",
                        JC:"结存数量",
                        FPrice:"单价(元)",
                        FAmount:"结存金额(元)",
                        FStoreSetNO:"仓库定置",
                        FDescription:"备注说明"
                    };

                    Index.renderPrintData($("#print"),data.rows,columnInfos);
                }
            });

            $gridList1.jqGrid('setFrozenColumns');

            $gridList1.jqGrid('setLabel', 0, '序号', 'rowNumberHeaderStyle');
        },
        renderPrintData:function(c,data,cols){
            c.empty();
            
            var rows = 16;

            var item = {
                page:1,maxPage:1,title:$("#wrapper h1").text() ,
                user:Index.curUserName,
                date:Index.curDate,
                time:Index.curTime,
                table:null
            };

            item = $.extend(item,Index.jsonData);

            var max = data.length;

            var maxPage = Math.ceil(max/rows);

            item.maxPage = maxPage;

            for(var i = 0;i < maxPage;i++){
                var colunms = [];

                var start = i*rows,end=(i+1)*rows;

                var table = $("<table>"),pageItem = $("<div>").addClass("page");

                end>max && (end=max);

                item.page = i+1;
                
                for(var j = start;j<end; j++){
                    var cur = data[j];

                    !colunms.length && (function(){
                        var title = $("<tr>");

                        for(var idx in cols){
                            colunms.push(idx);
                            
                            $("<th>").text(cols[idx]).appendTo(title);
                        }

                        title.appendTo(table);
                    })();

                    !!colunms.length && (function(){
                        var row = $("<tr>");

                        row.attr("rowData",JSON.stringify(cur));

                        for(var idx in colunms){
                            $("<td>").text(cur[colunms[idx]]).appendTo(row);
                        }

                        row.appendTo(table);
                    })();
                }
                
                item.table = "<table>{0}</table>".format(table.html());

                pageItem.html(Index.printItemTemplate.format(item)).appendTo(c);
            }
        }
    };
</script>

<div class="layout" id="layout" style="height: 100%; width: 100%;">
    <h1>XXXX年XX月 仓库容器明细账</h1>
    <header>
        <div class="btn-group">
            <a class="btn btn-primary center-block" onclick="$.reload()"><span class="glyphicon glyphicon-refresh"></span>&nbsp刷新</a>
            <a id="butnPrint1" class="btn btn-primary center-block"><span class="glyphicon glyphicon-print"></span>&nbsp打印</a>
            <a id="butnExport1" class="btn btn-primary center-block"><span class="glyphicon glyphicon-share"></span>&nbsp导出</a>
            <a id="butnBack1" class="btn btn-primary center-block"><span class="glyphicon glyphicon-chevron-left"></span>&nbsp;	返回</a>
        </div>
    </header>
    <section class="container">
        <section>
            <div class="col">
                <label>容器编号</label>
                <input id="FContainerNO" name="" placeholder="" type="text" class="form-control required" />
            </div>
            <div class="col">
                <label>容器名称	</label>
                <input id="FContainerName" name="" placeholder="" type="text" class="form-control required" />
            </div>
        </section>
        <aside>
            <div class="col">
                <label>
                    核算单价(元)     
                </label>
                <input id="FPrice" name="" placeholder="" type="text" class="form-control required" />
            </div>
            <div class="col">
                <label>最高储备(件)</label>
                <input id="FHighestStore" name="" placeholder="" type="text" class="form-control required" />
            </div>
            <div class="col">
                <label>最低储备(件)</label>
                <input id="FLowestStore" name="" placeholder="" type="text" class="form-control required" />
            </div>
        </aside>
    </section>
    <div class="grid_list_1_c">
        <table id="gridList1"></table>
        <div id="gridPager1"></div>
    </div>
    <div id="print" class="print">
        <div class="page">
            <h1>{title}</h1>
            <header>
                <div class="input_area">
                    <h2 class="input_title">容器名称</h2>
                    <div class="input_c input_c_w20">
                        {FContainerName}
                    </div>
                </div>
                <div class="input_area">
                    <h2 class="input_title">容器编号</h2>
                    <div class="input_c  input_c_w15">
                        {FContainerNO}
                    </div>
                </div>
                <div class="input_area">
                    <h2 class="input_title">核算单价</h2>
                    <div class="input_c input_c_w12">
                        {FPrice}
                    </div>
                    <h2 class="input_title_after">元</h2>
                </div>
                <div class="input_area">
                    <h2 class="input_title">最高储备</h2>
                    <div class="input_c input_c_w12">
                        {FHighestStore}
                    </div>
                    <h2 class="input_title_after">件</h2>
                </div>
                <div class="input_area">
                    <h2 class="input_title">最低储备</h2>
                    <div class="input_c input_c_w12">
                        {FLowestStore}
                    </div>
                    <h2 class="input_title_after">件</h2>
                </div>
                <div class="input_area">
                    <h2 class="input_title">容器分类</h2>
                    <div class="input_c input_c_w15">
                        {FContainerTypeName}
                    </div>
                </div>
                <div class="right_page">第{page}页，共{maxPage}页</div>
            </header>
            <div class="table_c">
                {table}
            </div>
            <footer>
                <span>打印人员：<label>{user}</label></span> <span>打印日期：<label>{date}</label><label>{time}</label></span>
            </footer>
        </div>
    </div>
</div>

